package com.coolH.service.serviceImpl;

import com.coolH.dao.FileMapper;
import com.coolH.entity.File;
import com.coolH.entity.Pager;
import com.coolH.entity.Student;
import com.coolH.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;


/**
 * Created by hbl on 2017/6/2.
 */
@Service("FileService")
public class FileServiceImpl implements FileService{
    @Autowired
    FileMapper fileMapper;

    @Override
    public void addFile(File file) {
        fileMapper.insert(file);
    }

    @Override
    public List<File> getById(Integer studentId) {
        return fileMapper.selectByStudent(studentId);
    }

    @Override
    public void getByPage(HttpServletRequest request, Model model, Integer userId) {
        String pageNow = request.getParameter("pageNow");

        Pager pager = null;

        Student students  = null;

        List<File> files = new ArrayList<File>();

        int totalCount =  (fileMapper.selectByStudent(userId)).size();


        if (pageNow != null) {
            pager = new Pager(totalCount, Integer.parseInt(pageNow));
            files = fileMapper.selectWithPage(pager.getStartPos(), pager.getPageSize(), userId);
        } else {
            pager = new Pager(totalCount, 1);
            files = fileMapper.selectWithPage(pager.getStartPos(), pager.getPageSize(), userId);
        }

        model.addAttribute("pages", files);
        model.addAttribute("page", pager);
    }

    @Override
    public void getBytxt(HttpServletRequest request, Model model, Integer userId, String txt) {
        String pageNow = request.getParameter("pageNow");

        Pager pager = null;

        Student students  = null;

        List<File> files = new ArrayList<File>();



        int totalCount =  (fileMapper.selectByStudent(userId)).size();


        if (pageNow != null) {
            pager = new Pager(totalCount, Integer.parseInt(pageNow));
            files = fileMapper.selectWithPage(pager.getStartPos(), pager.getPageSize(), userId);
        } else {
            pager = new Pager(totalCount, 1);
            files = fileMapper.selectWithPage(pager.getStartPos(), pager.getPageSize(), userId);
        }

        model.addAttribute("pages", files);
        model.addAttribute("page", pager);
    }

}
